﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using log4net;
using Model;


namespace Service
{
    public class FileService: IFile
    {
        //private System.ComponentModel.Container components = null;
        //protected override void Dispose(bool disposing)
        //{
        //    if (disposing)
        //    {
        //        if (components != null)
        //        {

        //            components.Dispose();
        //        }
        //    }
        //    base.Dispose(disposing);
        //}

        private static readonly ILog log = LogManager.GetLogger(typeof(Nullable));

        public List<EntitySreen> SeanConfig()//航信屏幕兼容性
        {
            List<EntitySreen> data = new List<EntitySreen>();
            EntitySreen Model = null;
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Print.exe.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                Model = new EntitySreen();
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                Model.Key = nodes[i].Attributes["key"].Value;
                Model.Value = nodes[i].Attributes["value"].Value;
                data.Add(Model);
            }
            return data;
        }

        public  string SeanTitle()//航信版本兼容性
        {
            //List<string> Data = new List<string>();
            //FileStream F = new FileStream(AppDomain.CurrentDomain.BaseDirectory.ToString()+ "Handler.txt",FileMode.Open);
            //StreamReader S = new StreamReader(F,Encoding.Default);

            //Data.Add(S.ReadToEnd());

            XmlDocument DOC = new XmlDocument();
            string fileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Print.exe.config";
            DOC.Load(fileName);
            XmlNodeList node = DOC.GetElementsByTagName("compatibilityData");

            // XmlAttribute att = node[0].Attributes["value"];
            return node[0].Attributes["value"].Value;
        }

        public List<EntitySreen> SeanInterface()//Web接口
        {
            List<EntitySreen> data = new List<EntitySreen>();
            EntitySreen Model = null;
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Print.exe.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("Ireturn");
            for (int i = 0; i < nodes.Count; i++)
            {
                Model = new EntitySreen();
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                Model.Key = nodes[i].Attributes["key"].Value;
                Model.Value = nodes[i].Attributes["value"].Value;
                data.Add(Model);
            }
            return data;
        }

        public  void InsertXML(string[] data ,string Node)
        {



            XmlDocument doc = new XmlDocument();

            XmlReaderSettings b = new XmlReaderSettings();
            b.IgnoreComments = true;
            XmlReader c = XmlReader.Create(Application.StartupPath + "\\Gingkoo.xml");

            doc.Load(c);

            XmlNode create = doc.SelectSingleNode(Node);
            XmlElement node = doc.CreateElement("GoingkooHangxin");

            XmlElement name = doc.CreateElement("Company");
            name.InnerText = data[0];

            XmlElement name1 = doc.CreateElement("Version");
            name1.InnerText = data[1];

            //XmlElement name2 = doc.CreateElement("Tax");
            //name2.InnerText = data.Tax;

            //XmlElement name3 = doc.CreateElement("StaffNo");
            //name3.InnerText = data.StaffNo;

            //XmlElement name4 = doc.CreateElement("StaffName");
            //name4.InnerText = data.StaffName;

            node.AppendChild(name);
            node.AppendChild(name1);
            //node.AppendChild(name2);
            //node.AppendChild(name3);
            //node.AppendChild(name4);
            //create.AppendChild(node);
            doc.Save(Application.StartupPath + "\\Gingkoo.xml");
            c.Close();
          //  Dispose(false);
        }

        public List<EntityStaff> SelectXML()
        {
            List<EntityStaff> ListOffLine=null;
       
                ListOffLine = new List<EntityStaff>();
                XmlDocument xml = new XmlDocument();

                XmlReaderSettings b = new XmlReaderSettings();
                b.IgnoreComments = true;
                XmlReader c = XmlReader.Create(Application.StartupPath + "\\Gingkoo.xml");

                xml.Load(c);

                XmlNode node = xml.SelectSingleNode("data");

                XmlNodeList list = node.ChildNodes;

                foreach (XmlNode item in list)
                {
                    EntityStaff model = new EntityStaff();
                    model.StaffName = item["StaffName"].InnerText;
                    model.StaffNo = item["StaffNo"].InnerText;
                    model.InvNo = item["InvNo"].InnerText;
                    model.Money = item["Money"].InnerText;
                    model.Tax = item["Tax"].InnerText;

                    ListOffLine.Add(model);
                }
                c.Close();


           // Dispose(false);


            return ListOffLine;
         

            //foreach (XmlNode item in list)
            //{
            //    EntityStaff model = new EntityStaff();
            //    XmlElement element = (XmlElement)item;

            //    XmlNodeList xn10 = element.ChildNodes;

            //    model.InvNo = xn10.Item(0).InnerText;

            //}
        }

        public void RemoveXMLItem(EntityStaff model)
        {
            XmlDocument xd = new XmlDocument();

            XmlReaderSettings b = new XmlReaderSettings();
            b.IgnoreComments = true;
            XmlReader c = XmlReader.Create(Application.StartupPath + "\\Gingkoo.xml");

            xd.Load(c);

            //XmlNode node = xd.SelectSingleNode("data");

            //XmlNodeList list = node.ChildNodes;

            //foreach (XmlNode item in list)
            //{
            //    if (item["InvNo"].InnerText == model.InvNo)
            //    {
            //        xd.RemoveChild(item);
            //    } 
            //}
            //xd.Save(Application.StartupPath + "\\Gingkoo.xml");
            //c.Close();

            XmlElement xe = xd.DocumentElement;
            foreach (XmlNode xn in xe.ChildNodes)
            {
                if (xn["InvNo"].InnerText == model.InvNo)
                {
                    xe.RemoveChild(xn);
                }
            }
            c.Close();
            xd.Save(Application.StartupPath + "\\Gingkoo.xml");
           // Dispose(false);

        }

        public List<EntitySreen> BWSeanConfig()//百旺屏幕兼容性
        {
            List<EntitySreen> data = new List<EntitySreen>();
            EntitySreen Model = null;
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Print.exe.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("bwdata");
            for (int i = 0; i < nodes.Count; i++)
            {
                Model = new EntitySreen();
                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                Model.Key = nodes[i].Attributes["key"].Value;
                Model.Value = nodes[i].Attributes["value"].Value;
                data.Add(Model);
            }
            return data;
        }

        public Dictionary<string, string> OCRSeanConfig()//图片识别数据
        {
            Dictionary<string, string> data = new Dictionary<string, string>();

            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Print.exe.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName("ocrdata");
            for (int i = 0; i < nodes.Count; i++)
            {

                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                data.Add(nodes[i].Attributes["key"].Value, nodes[i].Attributes["value"].Value);
            }
            return data;
        }

        public List<string> BitmapSean()//图片搜索
        {
            List<string> data = new List<string>();

            string dir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;


            DirectoryInfo dirInfo = new DirectoryInfo(dir);


            FileInfo[] files = dirInfo.GetFiles("*.png", SearchOption.TopDirectoryOnly);//当前目录
            List<int> test = new List<int>();
            foreach (FileInfo file in files)
            {
                var g = file.Name.Split(new char[] { '.' });
                int a = Convert.ToInt32(g[0]);
                test.Add(a);
                // data.Add(AppDomain.CurrentDomain.SetupInformation.ApplicationBase+file);
            }
            test.Sort();
            for (int i = 0; i < test.Count; i++)
            {
                data.Add(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + test[i] + ".png");
            }
            return data;


        }

        public Dictionary<string, string> SeanConfigByWhere(string node)//Key.Value
        {
            Dictionary<string, string> data = new Dictionary<string, string>();

            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径
            string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "Print.exe.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素
            XmlNodeList nodes = doc.GetElementsByTagName(node);
            for (int i = 0; i < nodes.Count; i++)
            {

                //获得将当前元素的key属性
                XmlAttribute att = nodes[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素
                data.Add(nodes[i].Attributes["key"].Value, nodes[i].Attributes["value"].Value);
            }
            return data;
        }

        public List<string> ReeadTXT(string Tax)
        {

            using (StreamReader sr = new StreamReader(Application.StartupPath + "\\"+Tax+".txt", Encoding.UTF8))
            {
                List<string> DATA = new List<string>();
               String line;
                while ((line = sr.ReadLine()) != null)
                {
                    DATA.Add(line.ToString());
                }
                return DATA;
            } 
        }

        public void WriteTXT(string path,List<string>DATA)
        {
            FileStream fs = new FileStream(Application.StartupPath + "\\"+path+".txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            for (int i = 0; i < DATA.Count; i++)
            {
                sw.WriteLine(DATA[i]);
            }
        
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }
    }
}
